#include <sys/socket.h>
//
// Created by root on 24-7-21.
//
static char readbyte(int client_fd) {
    char data;
    int a = recv(client_fd, &data, 1, 0);
    if (a == 0) {
        return 0;
    }
    return data;
}

int readline(int client_fd, char buffer[], unsigned int buffer_len) {
    unsigned int offset = 0;
    char lastchar = 0;

    while (1) {
        if (offset == buffer_len) {
            return offset - 1;
        }
        char c = readbyte(client_fd);
        if (c == 0) {
            return 0;
        }
        buffer[offset] = c;

        if (lastchar == '\r' && buffer[offset] == '\n') {
            return offset;
        }
        lastchar = buffer[offset];
        offset++;
    }
}
